package org.example.controller;
/*
* fzl
* */

import com.fasterxml.jackson.annotation.JsonFormat;
import org.example.domain.entity.Product;
import org.example.model.Result;
import org.example.model.SearchDateModel;
import org.example.service.ProductService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;

@RestController
@RequestMapping("/product")
@CrossOrigin("*")
public class ProductController {

    //获取商品及评论,图片等所有商品信息
    @Resource
    private ProductService productService;
    @GetMapping("/all")
    public Result getProducts(){
        return Result.buildSuccess(productService.getProducts());
    }

    //仅按页获取商品本身信息
    @GetMapping("/page")
    public Result getByPage(@RequestParam(value="page",defaultValue = "1",required = false) Integer page,@RequestParam(value="limit",defaultValue = "10",required = false) Integer limit) {
        return Result.buildSuccess(productService.getByPage(page,limit));
    }

    @PostMapping("/update")
    public Result updateImg(@RequestBody Product pro){
        Boolean b = productService.updateProduct(pro);
        if(b){
            return Result.buildSuccess(null);
        }else{
            return Result.buildFail("修改失败");
        }
    }
    @GetMapping("/delete")
    public Result deleteImg(@RequestParam Integer productId){
        Boolean b = productService.deleteProduct(productId);
        if(b){
            return Result.buildSuccess(null);
        }else{
            return Result.buildFail("删除失败");
        }
    }
    @PostMapping("/insert")
    public Result insertImg(@RequestBody Product pro){
        Boolean b = productService.insertProduct(pro);
        if(b){
            return Result.buildSuccess(null);
        }else{
            return Result.buildFail("新增商品失败");
        }
    }

    //搜索功能接口
    @GetMapping("/searchId")
    public Result getById(@RequestParam("productId") Integer productId) {
        return Result.buildSuccess(productService.getProductsById(productId));
    }
    @GetMapping("/searchName")
    public Result getByName(@RequestParam("productName") String productName,@RequestParam(value="page",defaultValue = "1",required = false) Integer page,@RequestParam(value="limit",defaultValue = "10",required = false) Integer limit) {
        return Result.buildSuccess(productService.getProductsByName(productName,page,limit));
    }
    @PostMapping("/searchCreateTime")
    public Result getByCreateTime(@RequestBody SearchDateModel createTime) {
        Date[] time = new Date[2];
        time[0] = createTime.getBeginTime();
        time[1] = createTime.getEndTime();
        return Result.buildSuccess(productService.getProductsByCreateTime(time,createTime.getPage(), createTime.getLimit()));
    }
    @PostMapping("/searchUpdateTime")
    public Result getByUpdateTime(@RequestBody SearchDateModel updateTime) {
        Date[] time = new Date[2];
        time[0] = updateTime.getBeginTime();
        time[1] = updateTime.getEndTime();
        return Result.buildSuccess(productService.getProductsByUpdateTime(time,updateTime.getPage(),updateTime.getLimit()));
    }
}
